﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;

namespace WpfApplication1
{
    /// <summary>
    /// Days.xaml 的交互逻辑
    /// </summary>
    //日期控件
    public partial class Days : UserControl
    {
        public int day_num = 0;
        public double start_time = 0;
        public Point position;
        public bool beSelected = false;
        static Random ran = new Random();
        static public Storyboard Detail=new Storyboard();
        static public Storyboard Brief=new Storyboard();
        static public Storyboard flashStory = new Storyboard();
        static public Window1 father;
        static public DateTime SelectedDay;
        public Days(int d,Point p,String name)
        {
            InitializeComponent();

            #region 初始化参数
            this.Width = 0;
            this.Height = 0;
            this.Margin = new Thickness(p.X, p.Y, 0, 0);
            this.HorizontalAlignment = HorizontalAlignment.Left;
            this.VerticalAlignment = VerticalAlignment.Top;
            position = p;
            day_num = d;
            this.Name = name;
            label_day.Content = day_num.ToString();
            #endregion

            #region 初始化动画效果
            NameScope.SetNameScope(this, new NameScope());
            this.RegisterName(this.Name, this);

            NameScope.SetNameScope(this.label_day, new NameScope());
            this.RegisterName(this.label_day.Name, this.label_day);

            

            DoubleAnimation onShow1 = new DoubleAnimation(0, 32, new Duration(TimeSpan.FromMilliseconds(500)));
            Storyboard.SetTargetName(onShow1, this.Name);
            Storyboard.SetTargetProperty(onShow1, new PropertyPath(Days.HeightProperty));

            DoubleAnimation onShow2 = new DoubleAnimation(0, 32, new Duration(TimeSpan.FromMilliseconds(500)));
            Storyboard.SetTargetName(onShow2, this.Name);
            Storyboard.SetTargetProperty(onShow2, new PropertyPath(Days.WidthProperty));
            
            ColorAnimation onFlash = new ColorAnimation(Color.FromRgb((byte)80, (byte)185, (byte)208), Color.FromRgb((byte)255, (byte)255, (byte)255), new Duration(TimeSpan.FromMilliseconds(500)));
            Storyboard.SetTargetName(onFlash, this.label_day.Name);
            Storyboard.SetTargetProperty(onFlash, new PropertyPath("Foreground.Color"));
            onFlash.RepeatBehavior = RepeatBehavior.Forever;

            Storyboard myStoryboard = new Storyboard();
            myStoryboard.Children.Add(onShow1);
            myStoryboard.Children.Add(onShow2);
           
            flashStory.Children.Add(onFlash);

            this.Loaded += delegate(object sender, RoutedEventArgs e)
            {
                onShow1.From = 0;
                onShow1.To = 32;
                onShow2.From = 0;
                onShow2.To = 32;
                myStoryboard.Begin(this);
            };

            this.MouseEnter += delegate(object sender, MouseEventArgs e)
            {
                if (!father.IsSelectFirst)
                {
                    if (!father.IsSelectedDay)
                    {
                        label_day.Foreground = new SolidColorBrush(Color.FromRgb((byte)80, (byte)185, (byte)208));
                        father.updateDayDetails(day_num);
                        Detail.Begin(father);
                    }
                }
                else
                {
                    label_day.Foreground = new SolidColorBrush(Color.FromRgb((byte)220, (byte)10, (byte)10));
                }
            };

            this.MouseLeave += delegate(object sender, MouseEventArgs e)
            {
                if (!father.IsSelectFirst)
                {
                    if (!father.IsSelectedDay)
                    {
                        Brief.Begin(father);
                        label_day.Foreground = new SolidColorBrush(Color.FromRgb((byte)0, (byte)0, (byte)0));
                    }
                }
                else
                {
                    label_day.Foreground = new SolidColorBrush(Color.FromRgb((byte)0, (byte)0, (byte)0));
                }
            };

            this.MouseDown += delegate(object sender, MouseButtonEventArgs e)
            {
                if (!father.IsSelectFirst)
                {

                    if (!father.IsSelectedDay)
                    {
                        SelectedDay = new DateTime(Months.now.Year, Months.now.Month, day_num);
                        flashStory.Begin(this);
                        beSelected = true;
                        father.IsSelectedDay = true;
                    }
                    else if (beSelected) 
                    {
                        flashStory.Stop(this);
                        label_day.Foreground = new SolidColorBrush(Color.FromRgb((byte)0, (byte)0, (byte)0));
                        beSelected = false;
                        father.IsSelectedDay = false;
                    }
                }
                else
                {
                    father.IsSelectFirst = false;
                    label_day.Foreground = new SolidColorBrush(Color.FromRgb((byte)0, (byte)0, (byte)0));
                    RecordEveryday.StartDay = new DateTime(Months.now.Year, Months.now.Month, day_num);
                }
            };
            #endregion
        }


    }
}
